#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 胡浩浩
# datetime： 2021/12/27 14:39 
# ide： PyCharm
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DATETIME
from sqlalchemy.orm import relationship
from Tools.database import Base

"""
    定义用户相关的数据模型
        用户表: user_info
            id              int             主键  is not null
            username        varchar(255)    用户名 
            password        varchar(255)    密码
            name            varchar(255)    姓名
            create_time     datetime        创建时间
            update_time     datetime        更新时间
            deleted         boolean         逻辑删除1:true 0:false
            remark          varchar(255)    备注信息
        用户组表：user_group
            id              int             主键  is not null 
            group_name      varchar(255)    用户组名称
            group_code      varchar(255)    用户组编码
            create_time     datetime        创建时间
            update_time     datetime        更新时间
            deleted         boolean         逻辑删除1:true 0:false
        用户和用户组关系表: user_group_relation
            id              int             主键  is not null
            user_id         int             用户id    
            group_id        int             用户组id
            deleted         boolean         逻辑删除1:true 0:false
            create_time     datetime        创建时间
            update_time     datetime        更新时间
"""


class User(Base):
    __tablename__ = "user_info"
    id = Column(Integer, primary_key=True, index=True, unique=True)
    username = Column(String(255), unique=True)
    password = Column(String(255))
    name = Column(String(255))
    create_time = Column(DATETIME)
    update_time = Column(DATETIME)
    deleted = Column(Boolean, default=False)
    # user_relation = relationship("UserGroupRelation", back_populates="user")


class Group(Base):
    __tablename__ = "user_group"
    id = Column(Integer, primary_key=True, index=True, unique=True)
    group_name = Column(String(255), unique=True)
    group_code = Column(String(255), unique=True)
    create_time = Column(DATETIME)
    update_time = Column(DATETIME)
    deleted = Column(Boolean, default=False)
    # group_relation = relationship("UserGroupRelation", back_populates="group")


class UserGroupRelation(Base):
    __tablename__ = "user_group_relation"
    id = Column(Integer, primary_key=True, index=True, unique=True)
    user_id = Column(Integer)
    group_id = Column(Integer)
    deleted = Column(Boolean, default=False)
    create_time = Column(DATETIME)
    update_time = Column(DATETIME)
    # user = relationship("User", back_populates="user_relation")
    # group = relationship("Group", back_populates="group_relation")
